﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using Maticsoft.DBUtility;
using Utils;
using System.Collections.Specialized;
using Maticsoft.Model;


public partial class shopcart_confirmation : FBasePage
{
    // Fields
    public NameValueCollection _system = new NameValueCollection();
    public Maticsoft.BLL.orderdetail detailbll = new Maticsoft.BLL.orderdetail();
    public List<orderdetail> detaillist = new List<orderdetail>();
    public bool falg = false;
    protected decimal fare = 0M;

    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public string orderid = "A007";
    public List<Order> orderlist = new List<Order>();
    public Order ordermodel = new Order();
    public string pagenum = "1";
    public bool result = false;
    public string service = "";
    public string strpage;
    public Maticsoft.BLL.System sysbll = new Maticsoft.BLL.System();
    public SystemConfig sysconfig = new SystemConfig();
    public Maticsoft.Model.System sysmodel = new Maticsoft.Model.System();
    public Maticsoft.BLL.Team teambll = new Maticsoft.BLL.Team();
    public Team teamodel = new Team();
    protected decimal totalprice = 0M;
    public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();

    // Methods
    public void AddReceive()
    {
        this.ordermodel = this.orderbll.GetModel(Convert.ToInt32(base.Request["orderid"]));
        this.ordermodel.Realname = base.Request["realname"];
        this.ordermodel.Mobile = base.Request["mobile"];
        this.ordermodel.Address = base.Request["address"];
        this.ordermodel.Zipcode = base.Request["zipcode"];
        string str = this.txtremark.Value;
        if (str.IndexOf(@"\") > 0)
        {
            str = str.Replace(@"\", @"\\");
        }
        this.ordermodel.Remark = str;
        this.orderbll.Update(this.ordermodel);
    }

    public void ConfirmOrder()
    {
        Order order = new Order();
        UserInfo model = new UserInfo();
        model = this.userbll.GetModel(this.userbll.Getuserid(base.UserName));
        order = this.orderbll.GetModel(Convert.ToInt32(this.hiorderid.Value));
        if (order != null)
        {
            if (model.Money < order.Origin)
            {
                if (base.Request["pd_FrpId"] != null)
                {
                    this.service = "yeepay";
                }
                else if (base.Request["paytype"] != null)
                {
                    this.service = base.Request["paytype"].ToString();
                }
                else
                {
                    this.service = "";
                }
                if (this.service == "")
                {
                    this.SetError("请您选择支付方式");
                    base.Response.Redirect("confirmation.aspx?orderid=" + order.Id);
                }
                else
                {
                    this.orderbll.updatebyservice(this.hiorderid.Value, this.service, this.orderbll.Getorder(order.User_id, order.Team_id, Convert.ToInt32(this.hiorderid.Value)), order.Origin);
                    base.Response.Redirect(string.Concat(new object[] { "pay.aspx?orderid=", order.Id, "&pd_FrpId=", base.Request["pd_FrpId"] }));
                }
            }
            else
            {
                this.service = "credit";
                this.orderbll.updatebyservice(this.hiorderid.Value, this.service, this.orderbll.Getorder(order.User_id, order.Team_id, Convert.ToInt32(this.hiorderid.Value)), order.Origin);
                base.Response.Redirect("pay.aspx?orderid=" + this.hiorderid.Value + "&pd_FrpId=" + base.Request["pd_FrpId"]);
            }
        }
        else
        {
            this.SetError("订单不存在");
        }
    }

    public void GetOrder()
    {
        UserInfo info = new UserInfo();
        this.ordermodel = this.orderbll.GetModel(Convert.ToInt32(this.hiorderid.Value));
        this.blockcard1.UpdateView(Convert.ToInt32(this.hiorderid.Value));
    }

    public void GetSystem()
    {
        this.sysmodel = this.sysbll.GetModel(1);
    }

    public string GetTeam(string id)
    {
        this.teamodel = this.teambll.GetModel(Convert.ToInt32(id));
        if (this.teamodel != null)
        {
            return this.teamodel.Title;
        }
        return "";
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this._system = this.sysconfig.GetSystem();
        if (!(this.Page.User.Identity.IsAuthenticated && !(this.Page.User.Identity.Name == "")))
        {
            base.Response.Redirect("/account/Login.aspx");
        }
        if (base.Request.Form["btnadd"] == "确认订单，付款")
        {
            if (!this.result)
            {
                if (!(base.IsLogin || !(base.UserName == "")))
                {
                    base.Response.Redirect("/account/Login.aspx");
                }
                this.AddReceive();
                CookieCar.ClearCar();
                this.ConfirmOrder();
            }
            else
            {
                this.SetError("订单已过期,请修改订单");
            }
        }
        if (base.Request["orderid"] != null)
        {
            if (Utility.IsNum(base.Request["orderid"].ToString()))
            {
                this.hiorderid.Value = base.Request["orderid"].ToString();
                this.ordermodel = this.orderbll.GetModel(Convert.ToInt32(this.hiorderid.Value));
                if (this.ordermodel != null)
                {
                    if (!this.Page.IsPostBack)
                    {
                    }
                    this.txtremark.Value = this.ordermodel.Remark;
                    Team model = new Maticsoft.BLL.Team().GetModel(this.ordermodel.Team_id);
                    if (model != null)
                    {
                        TeamState state = base.GetState(model);
                        if ((state != TeamState.begin) && (state != TeamState.successbuy))
                        {
                            this.SetError("该项目已过期或已卖光，不能支付！");
                            base.Response.Redirect("/index.aspx");
                            base.Response.End();
                            return;
                        }
                    }
                    this.GetOrder();
                    goto Label_025B;
                }
                this.SetError("订单不存在");
            }
            else
            {
                this.SetError("您输入的参数非法");
            }
            return;
        }
        base.Response.Redirect("/account/Login.aspx");
    Label_025B:
        this.GetSystem();
    }

}